// pages/info/info.js
import {
  updateUserAPI,
  getUserInfoAPI
} from '../../../api/user'


Page({
  data: {
    nickName: '',
    phoneNumber: '',
    email: '',
    userId: 0,
    username: ''
  },
  async onSubmit() {
    let user = {
      nickName: this.data.nickName,
      phoneNumber: this.data.phoneNumber,
      email: this.data.email,
      id: this.data.userId,
    }
    let that = this
    wx.showModal({
      title: '提示',
      content: '您确定要修改信息吗',
      success(res) {
        if (res.confirm) {
          updateUserAPI(user).then(res => {
            if (res.code == 200) {
              wx.showToast({
                title: '修改成功',
                icon: 'success',
                duration: 500
              })
              getUserInfoAPI(that.data.username).then(res => {
                wx.setStorageSync('user', res.result)
              })
              setTimeout(() => {
                wx.navigateBack()
              }, 500)
            }
          })
        } else if (res.cancel) {}
      }
    })


  },
  onLoad(options) {
    let user = wx.getStorageSync('user')
    if (user) {
      this.setData({
        nickName: user.nickName,
        phoneNumber: user.phoneNumber,
        email: user.email,
        userId: user.id,
        username: user.username,
      })
    }
  },
})